package letsjava.pattern.visitor;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

public class ConcreteVisitor implements Visitor {
	// 在本方法中,我们实现了对Collection的元素的成功访问
	public void visitCollection(Collection collection) {
		Iterator iterator = collection.iterator();
		while (iterator.hasNext()) {
			Object o = iterator.next();
			if (o instanceof Visitable)
				((Visitable) o).accept(this);
		}
	}

	public void visitString(StringElement stringE) {
		System.out.println("'" + stringE.getValue() + "'");
	}

	public void visitFloat(FloatElement floatE) {
		System.out.println(floatE.getValue().toString() + "f");
	}
	
	public static void main(String[] args){
		Visitor visitor = new ConcreteVisitor(); 
		StringElement stringE = new StringElement("I am a String"); 
		visitor.visitString(stringE);
		
		Collection list = new ArrayList(); 
		list.add(new StringElement("I am a String1")); 
		list.add(new StringElement("I am a String2")); 
		list.add(new FloatElement(new Float(12))); 
		list.add(new StringElement("I am a String3")); 
		visitor.visitCollection(list);
	}
}